<?php
namespace control;
//日志类
class logClass extends adIndexClass
{
	/*列表页-查询条件*/
	public function home()
	{
		$stid = '';
		if ($this->power("log", "seeAll")) {
			$stid = $this->staffSelect(array("show" => 4));
		}
		$html =
			select(array("name" => "powerOne", "title" => "{l.log.home.powerOne}", "option" => array_keys($this->powerAll), "change" => "getPowerTwo")) .
			select(array("name" => "powerTwo", "title" => "{l.log.home.powerTwo}")) .
			$stid .
			text(array("name" => "text", "title" => "{l.log.home.text}")) .
			text(array("name" => "startDay", "laydate" => [], "title" => "{l.log.home.startDay}")) . "-\n" .
			text(array("name" => "endDay", "laydate" => [], "title" => "{l.log.home.endDay}"));
		//返回
		return $this->homeFun($this->search($html));
	}
	
	/*列表页-查询结果*/
	public function homeSearch()
	{
		//like查询数组
		$like = array(
			"text" => $this->post('text'),//详细说明
		);
		//全等查询数组
		$equal = array(
			"stid" => $this->post('stid'),//目标对象
		);
		$and   = "";
		//导航
		if (!empty($this->post('powerOne'))) {
			if (empty($this->post('powerTwo'))) {
				$powerOne = $this->post('powerOne');
				$power    = [];
				foreach ($this->powerAll[$powerOne] as $array) {
                    unset($array['attribute']);
					foreach ($array as $key => $val) {
						$power[] = $key;
					}
				}
			} else {
				$powerOne = $this->post('powerOne');
				$powerTwo = $this->post('powerTwo');
				$power    = [];
				foreach ($this->powerAll[$powerOne] as $secName=>$array) {
                    unset($array['attribute']);
                    if ($secName == $powerTwo) {
                        foreach ($array as $key => $val) {
                            $power[] = $key;
                        }
                    }

				}
			}
			$and .= " and type in ( '" . implode("','", $power) . "' ) ";
		}
		//发生时间-开始日期
		if (!empty($this->post('startDay'))) {
			$startDay = $this->post('startDay') . " 00:00:00";
			$and      .= " and time >= '$startDay' ";
		}
		//记录时间-结束日期
		if (!empty($this->post('endDay'))) {
			$endDay = $this->post('endDay') . " 23:59:59";
			$and    .= " and time <= '$endDay' ";
		}
		//如果没有查看所有员工账户记录的权限
		if (!$this->power("log", "seeAll")) {
			$and .= " and stid = '" . $this->control['stid'] . "' ";
		}
		//获取查询结果
		$where = search(["like" => $like, "=" => $equal]) . $and;
		$page  = $this->page(["table" => "log", "where" => $where, "order" => "id desc"]);
		//获取表头
		// $tr[]     = array("一级导航", "二级导航", "三级导航", "员工", "日志", "时间");
		$tr[] = ['{l.log.homeSearch.menuOne}', '{l.log.homeSearch.menuTwo}', '{l.log.homeSearch.menuThree}', '{l.log.homeSearch.staffName}', '{l.log.homeSearch.text}', '{l.log.homeSearch.time}'];
		//获取记录
		foreach ($page['array'] as $array) {
			$menuOne = $menuTwo = $menuThree = "";
			foreach ($this->powerAll as $oneKey => $twoArray) {
				foreach ($twoArray as $twoKey => $threeArray) {
					if (isset($threeArray[$array['type']]['name']) && $threeArray[$array['type']]['name']) {
						$menuThree = $threeArray[$array['type']]['name'];
						$menuOne   = $oneKey;
						$menuTwo   = $twoKey;
					}
				}
			}
			$staff = query("staff", " stid = '$array[stid]' ");
			$text  = preg_replace(array("/\n/"), array("<br>"), $array['text']);
			$tr[]  = [$menuOne, $menuTwo, $menuThree, $staff['name'], $text, $array['time']];
		}
		//控件
		$button = "";
		if ($this->power("log", "del")) {
			$button .= "<span onclick=\"layer('log','del',{})\" class='spanButton'>" . L('log.homeSearch.deletespan') . "</span>";
		}
		//返回
		$json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'];
		return json_encode($json, 320);
	}
	
	/*弹出层-删除日志*/
	public function del()
	{
		$html = $this->layerEdit(array(
			"table" => array(
				array(
					"{l.log.del.warmTips}" => "{l.log.del.warmTipsMsg}",
				),
				array(
					must . "{l.log.del.selectDate}" => text(array("name" => "date", "laydate" => [])),
				),
				array(
					must . "{l.log.del.password}" => password(array("name" => "password")),
				),
			),
		));
		return $this->layer(array("title" => "{l.log.del.layTitle}", "height" => 260, "width" => 500, "html" => $html));
	}
	
	/*异步处理-删除日志*/
	public function delEdit()
	{
		$delTime = date("Y-m-d", strtotime($this->post('date'))) . " 00:00:00";
		if (empty($this->post('date'))) {
			$json['warn'] = L('log.delEdit.cleaningDate');
			// $json['warn'] = "请选择日志清理日期";
		} elseif (strtotime($delTime) > time() - 86400 * 30) {
			$json['warn'] = L('log.delEdit.keepALog');
			// $json['warn'] = "建议保留至少三十天的日志";
		} elseif (empty($this->post('password'))) {
			$json['warn'] = L('log.delEdit.inputPassword');
			// $json['warn'] = "请填写你的登录密码";
		} elseif (verifyPwd($this->post('password'), self::$user['password']) === false) {
			$json['warn'] = L('log.delEdit.passwordErr');
			// $json['warn'] = "登录密码输入有误";
		} else {
			//统计日志数量
			$num = sqlNum("log", " time < '$delTime' ");
			//删除日志
			if ($num == 0) {
				$json['warn'] = L('log.delEdit.noLog');
				// $json['warn'] = "此日期前一条日志都没有";
			} else {
				delete(array("table" => "log", "where" => " time < '$delTime' "));
				//添加日志
				$text = $this->control['name'] . "删除了" . $num . "条" . $this->post('date') . "日前的日志";
				$this->logText(array("type" => "log", "text" => $text));
				//提示
				$_SESSION['warn'] = $text;
				$json['warn']     = 2;
			}
		}
		//返回
		return json_encode($json, 320);
	}
}
